package com.lenovo.smarttraffic.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;

public class NetWorkActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText id1;
    private EditText id2;
    private EditText id3;
    private EditText id4;
    private Button ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_net_work);
        initView();
    }

    private void initView() {
        id1 = (EditText) findViewById(R.id.id1);
        id2 = (EditText) findViewById(R.id.id2);
        id3 = (EditText) findViewById(R.id.id3);
        id4 = (EditText) findViewById(R.id.id4);
        ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(this);
    }

    private void submit() {
        // validate
        String id1String = id1.getText().toString().trim();
        if (TextUtils.isEmpty(id1String)) {
            Toast.makeText(this, "id1String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String id2String = id2.getText().toString().trim();
        if (TextUtils.isEmpty(id2String)) {
            Toast.makeText(this, "id2String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String id3String = id3.getText().toString().trim();
        if (TextUtils.isEmpty(id3String)) {
            Toast.makeText(this, "id3String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String id4String = id4.getText().toString().trim();
        if (TextUtils.isEmpty(id4String)) {
            Toast.makeText(this, "id4String不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        Integer integer1 = Integer.valueOf(id1String);
        Integer integer2 = Integer.valueOf(id2String);
        Integer integer3 = Integer.valueOf(id3String);
        Integer integer4 = Integer.valueOf(id4String);

        if (integer1 > 255 || integer1 < 0) {
            Toast.makeText(this, "输入有误只能输入0-255", Toast.LENGTH_SHORT).show();
            id1.setText("");
        }
        if (integer2 > 255 || integer2 < 0) {
            Toast.makeText(this, "输入有误只能输入0-255", Toast.LENGTH_SHORT).show();
            id2.setText("");

        }
        if (integer3 > 255 || integer3 < 0) {
            Toast.makeText(this, "输入有误只能输入0-255", Toast.LENGTH_SHORT).show();
            id3.setText("");
        }
        if (integer4 > 255 || integer4 < 0) {
            Toast.makeText(this, "输入有误只能输入0-255", Toast.LENGTH_SHORT).show();
            id4.setText("");
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ok:
                submit();
                break;
        }
    }
}
